<?php

class RbacController extends Common {

    private $user;

    public function __construct() {
        parent::__construct();
//        $this->load->model('device');
        $this->user = $this->model('user');
    }

    /**
     * 列表
     */
    public function listAction() {
        $userid = $this->session->get('user_id');   //从session中获取用户id
        $page     = $this->get('page') ? $this->get('page') : 1;
        $pagesize = 10;
        $pagelist = $this->instance('pagelist');
        $pagelist->loadconfig();
        $where = ' roleid <> 1 and pid = '.$userid;
        $model = $this->model('user');
        $total = $model->count('user', 'userid', $where);
        $urlparam = array(
            'page' => '{page}',
        );
        $data = $model->page_limit($page, $pagesize)->order(array('userid DESC'))->where($where)->select();
        $pagelist = $pagelist->total($total)->url(url('admin/rbac/list', $urlparam))->num($pagesize)->page($page)->output();
        $this->view->assign(array(
            'page' => $page,
            'data' => $data,
            'pagelist'=> $pagelist,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/rbac/list');  //这里是对视图文件的加载
    }

    /**
     * 添加代理商
     */
    public function addAgentAction()
    {
        $userid = $this->session->get('user_id');   //从session中获取用户id
        if ($this->post('submit')) {
            $data['username'] = $_POST['username'];
            $pwd = $_POST['password'];   //密码
            $data['roleid'] = intval($_POST['roleid']);   //代理商级别
            $data['phone'] = $_POST['phone'];   //代理商手机号码
            $data['tel'] = $_POST['tel'];   //代理商固话
            $data['realname'] = $_POST['realname'];     //代理商真实姓名
            $data['note'] = $_POST['note'];     //备注
            $data['pid'] = $userid;     //所属上级代理商id
            if(empty($data['username']) || empty($pwd) || empty($data['phone']) || !in_array($data['roleid'],[2,3,4])){
                echo "代理商姓名、密码、手机号码、级别必填";exit;
            }
            if(!preg_match("/^1[34578][0-9]{9}/",$data['phone'])){
                echo '手机号码格式有误';exit;
            }
            $model = $this->model('user');
            $data['salt'] = '2d20355bf9';   //密码盐值
            $data['password'] = md5(md5($pwd) . $data['salt'] . md5($pwd));
            $data['create_time'] = $data['update_time'] = time();
//            var_dump($data);exit;
//            $insert_id = $model->insert($data);
            $isCreate = $this->user->createUser($data,$pwd);
            if(empty($isCreate)){
                $this->memberMsg('添加失败', url('admin/rbac/addAgent'));
            }
            $this->adminMsg('添加成功', url('admin/rbac/addAgent'), 3, 1, 1);
        }
        $userInfo = $this->user->userinfo($userid);     //获取用户信息
        $this->view->assign(array(
            'userInfo' => $userInfo,
        )); //assign()对视图变量完成赋值
        $this->view->display('/admin/rbac/add_agent');  //这里是对视图文件的加载
    }

    public function sfAction(){
        $username=$_POST[username];   //获取视图文件传过来的表单的值  表单我用post传值
        // $model = $this->model('member');  //实例化model 这里的两行是用fine的原有的写的，但是我感觉这样不灵活我下面又用php的写了因为我感觉还是php写的顺手，大家自己感觉什么好就用什么吧
        // $data = $model->getAll('username=?', "$username"); //用的fine的getall函数
        $sql="select * from fn_member where username='$username' ";   //sql语句  这里是php的
        $row=mysql_query($sql);  //php执行sql
        $data=mysql_fetch_assoc($row);  //获取sql语句得到的值
        $this->view->assign('data',$data); //assign()对视图变量完成赋值
        $this->view->display('sf'); //加载视图文件
    }

}
